<template>
  <div class="package__item">
    <span class="hot__tag" v-if="isHot"></span>
    <div class="package__content"
      :class="{ 'is-active': activeItemId === item.id }"
      @click="$emit('select', item)"
      >
      <div class="time__content fs14">
        <span class="time" v-if="item.cycle_pay_flag&&!item.unlimited"> 连续包{{ item.bus_ext.duration | cycleText }}</span>
        <span class="time" v-else> {{ item.name}}</span>
      </div>
      <div class="real-price fs24">
        <span class="fs16">￥</span>
        <em>{{  finalPrice | moneyToyuan }}</em>
      </div>
      <div class="origin-price">
        ￥{{ item.origin_price|moneyToyuan }}
      </div>
      <span class="nums__devices" v-if="!item.unlimited">低至￥{{ minPricePerDay | moneyToyuan }}/天</span>
      <span class="nums__devices" v-else>永久使用</span>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  props: {
    item: {
      type: Object,
      default: () => {}
    },
    userInfo: {
      type: Object,
      default: () => {}
    },
    activeItemId: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
    }
  },
  computed: {
    finalPrice () {
      const actByProduct = this.item['10004']?.[0]
      if (actByProduct) {
        return this.userInfo.has_buy
          ? Math.min(actByProduct.old_user_price, this.item.price)
          : Math.min(actByProduct.new_user_price, this.item.price)
      }
      return this.item.price
    },
    minPricePerDay () {
      return (this.finalPrice / (this.item.bus_ext.duration / 24 / 60)).toFixed(2)
    },
    isHot () {
      const days = this.item.bus_ext.duration / 24 / 60
      return days === 365 || days === 366
    }
  },
  filters: {
    moneyToyuan (val = 0) {
      return parseFloat((val / 100).toFixed(2))
    },
    valToFixed2 (val = 0) {
      return (val / 100).toFixed(2)
    },
    cycleText (duration) {
      let num = 0
      if (duration % (duration / 365 / 24 / 60) === 0) {
        num = duration / 365 / 24 / 60
        return (num === 1 ? '' : num) + '年'
      }
      if (duration % (duration / 30 / 24 / 60) === 0) {
        num = duration / 30 / 24 / 60
        if (num === 1) {
          return '月'
        }
        if (num === 3) {
          return '季'
        }
        if (num === 6) {
          return '半年'
        }
        return num + '月'
      }
      num = duration / 24 / 60
      return (num === 1 ? '' : num) + '天'
    }
  }
}
</script>

<style lang="scss" scoped>
.package__item{
  padding-top: 5px;
}
.package__content {
  width: 100%;
  height: 160px;
  // flex: 0 0 190px;
  border-radius: 10px;
  text-align: center;
  background-color: rgba($color: #fff, $alpha: 0.05);
  box-sizing: border-box;
  overflow: hidden;
  cursor: pointer;
  .time__content {
    background-color: rgba($color: #fff, $alpha: 0.1);
    color:#fff;
    font-weight: bold;
    line-height: 36px;
  }
  .real-price {
    margin-top: 12px;
    color: #fff;
    font-size: 24px;
    font-weight: bold;
    white-space: nowrap;
    span {
      margin-right: 2px;
      font-size: 16px;
      padding-bottom: 4px;
    }
  }
  .origin-price {
    margin-top: 6px;
    font-size: 12px;
    color: rgba($color:#fff, $alpha: 0.5);
    text-decoration: line-through;
  }
  .nums__devices {
    display: inline-block;
    margin: 14px auto 0 auto;
    padding: 0 10px;
    height: 26px;
    min-width: 98px;
    line-height: 26px;
    background: rgba($color: #fff, $alpha: 0.1);
    color: #fff;
    border-radius: 20px;
    font-size: 12px;
    text-align: center;

  }
}
.package__content:hover {
  background-color: rgba($color: #fff, $alpha: 0.1);
}
.package__content.is-active {
  background: linear-gradient( 180deg, #FFF2D8 0%, #FFE3AC 100%);
  .time__content {
    background: linear-gradient( 132deg, #FFA15E 0%, #FF419C 100%);
  }
  .real-price {
    color: #7B4801;
  }
  .origin-price {
    color: rgba($color:#7B4801, $alpha: 0.5);
  }
  .nums__devices {
    background: rgba($color: #7B4801, $alpha: 0.1);
    color: #7B4801;
  }
}
.hot__tag {
  position: absolute;
  top: 2px;
  right: -4px;
  display: block;
  width: 54px;
  height: 54px;
  background: url(~@/assets/hot-tag.png) no-repeat center center / 100% 100%;
  color:#fff;
  span {
    display: block;
    transform: rotate(45deg);
    white-space: nowrap;
    position: relative;
    top: 11px;
    left: 9px;
    font-size: 12px;
  }
}
</style>
